package io.renren.common.xss;

import com.alibaba.druid.support.json.JSONUtils;
import com.alibaba.fastjson.JSONObject;
import io.renren.common.utils.HttpContextUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.io.IOException;
import java.util.Map;

public class LoggingFilter implements Filter {

    Logger logger = LoggerFactory.getLogger(getClass());

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
       String filterName =  filterConfig.getFilterName();
       logger.info("filterName={}",filterName);
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest loggingRequest = (HttpServletRequest)servletRequest;
        StringBuffer url = loggingRequest.getRequestURL();
        Map paramsMap = HttpContextUtils.getParameterMap(loggingRequest);
        logger.info("paramsMap={}", new JSONObject(paramsMap).toJSONString());
        paramsMap = loggingRequest.getParameterMap();
        Map headMap = HttpContextUtils.getHeaderMap(loggingRequest);
        String authType = loggingRequest.getAuthType();
        logger.info("url={}",url);
        logger.info("authType={}",authType);
        logger.info("paramsMap={}", new JSONObject(paramsMap).toJSONString());
        logger.info("headMap={}", new JSONObject(headMap).toJSONString());
        filterChain.doFilter(loggingRequest, servletResponse);
    }


}
